﻿//
using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Reflection;

namespace WorkAByte
{
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class DataTableDataSource : DataSourceControl
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Page.RegisterRequiresControlState(this);
        }
        public DataTableDataSource() : base() { }

        public DataTable DataSource
        {
            get
            {
                return ((DataTableDataSourceView)this.GetView(String.Empty)).SourceData;
            }
            set
            {
                if (((DataTableDataSourceView)this.GetView(String.Empty)).SourceData != value)
                {
                    ((DataTableDataSourceView)this.GetView(String.Empty)).SourceData = value;
                    RaiseDataSourceChangedEvent(EventArgs.Empty);
                }
            }
        }

        private DataTableDataSourceView view = null;
        protected override DataSourceView GetView(string viewName)
        {
            if (null == view)
            {
                view = new DataTableDataSourceView(this, String.Empty);
            }
            return view;
        }

        protected override ICollection GetViewNames()
        {
            ArrayList al = new ArrayList(1);
            al.Add(DataTableDataSourceView.DefaultViewName);
            return al as ICollection;
        }

        protected override object SaveControlState()
        {
            object objBaseState = base.SaveControlState();
            if (DataSource != null)
            {
                if (objBaseState != null)
                    return new Pair(objBaseState, this.DataSource);
                else
                    return (this.DataSource);
            }
            else
                return objBaseState;
        }

        protected override void LoadControlState(object cState)
        {
            if (cState != null)
            {
                Pair p = cState as Pair;
                if (p != null)
                {
                    base.LoadControlState(p.First);
                    this.DataSource = (DataTable)p.Second;
                }
                else
                {
                    if (cState is DataTable)
                        this.DataSource = (DataTable)cState;
                    else
                        base.LoadControlState(cState);
                }
            }
        }
    }

    public class DataTableDataSourceView : DataSourceView
    {
        public DataTableDataSourceView(IDataSource owner, string name) : base(owner, DefaultViewName) { }

        public static string DefaultViewName = "CommaSeparatedView";

        private DataTable _SourceData;
        internal DataTable SourceData
        {
            get { return _SourceData; }
            set { _SourceData = value; }
        }

        protected override IEnumerable ExecuteSelect(DataSourceSelectArguments selectArgs)
        {
            IEnumerable dataList = null;
            DataView dataView = new DataView(SourceData);

            if (selectArgs.SortExpression != String.Empty)
                dataView.Sort = selectArgs.SortExpression;

            dataList = dataView;
            return dataList;
        }

        public override bool CanDelete
        {
            get
            {
                return false;
            }
        }
        protected override int ExecuteDelete(IDictionary keys, IDictionary values)
        {
            throw new NotSupportedException();
        }
        
        public override bool CanInsert
        {
            get
            {
                return true;
            }
        }
        protected override int ExecuteInsert(IDictionary values)
        {
            DataRow newRow = this.SourceData.NewRow();
            foreach (DictionaryEntry aValue in values)
                newRow[aValue.Key.ToString()] = aValue.Value;
            this.SourceData.Rows.Add(newRow);
            return values.Count;
        }
        
        public override bool CanUpdate
        {
            get
            {
                return true;
            }
        }
        protected override int ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues)
        {
            StringBuilder sbSelect = new StringBuilder();
            foreach (DictionaryEntry aValue in keys)
                sbSelect.Append(string.Format("{0} = {1}", aValue.Key, aValue.Value));

            DataRow[] rowsToUpdate = this.SourceData.Select(sbSelect.ToString());
            foreach (DataRow aRow in rowsToUpdate)
                foreach (DictionaryEntry aValue in values)
                    aRow[aValue.Key.ToString()] = aValue.Value;

            return rowsToUpdate.Length;
        }

        private static void OutPut(string ValueToPrint)
        { System.Diagnostics.Debug.WriteLine(ValueToPrint); }
    }


}